package com.itheima.alipay;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class MainActivity extends AppCompatActivity {
    //    http://localhost:8080/HeiMaPay/Pay?goodId=111&count=1&price=0.01

    public String baseUrl = "http://10.0.2.2:8080/HeiMaPay/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void checkout(View view) {
        //第一步，提交信息到后台服务器
        new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiService.class)
                .submit("111", "1", "0.01")
                .enqueue(new Callback<OrderInfo>() {
                    @Override
                    public void onResponse(Call<OrderInfo> call, Response<OrderInfo> response) {
                        if (response.isSuccessful()) {
                            //第二步，获取订单签名信息
                            OrderInfo orderInfo = response.body();
                            Toast.makeText(MainActivity.this, orderInfo.toString(), Toast.LENGTH_SHORT).show();

                            //第三步：调用支付宝SDK进行支付
                            startPay(orderInfo.getPayInfo());
                        }
                    }

                    @Override
                    public void onFailure(Call<OrderInfo> call, Throwable t) {

                    }
                });
    }

    //第三步：调用支付宝SDK进行支付
    public void startPay(final String payInfo) {
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(MainActivity.this);
                Map<String, String> result = alipay.payV2(payInfo, true);

                Message msg = new Message();
                msg.what = 0;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };
        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    //第四步：获取支付结果
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Map<String, String> result = (Map<String, String>) msg.obj;

            String memo = result.get("memo");//描述信息
            String result2 = result.get("result");//是处理结果
            String resultStatus = result.get("resultStatus");//结果码(类型为字符串)

            Toast.makeText(MainActivity.this, resultStatus,
                    Toast.LENGTH_LONG).show();
        }

        ;
    };


    public interface ApiService {
        @GET("Pay")
        Call<OrderInfo> submit(
                @Query("goodId") String goodId,
                @Query("count") String count,
                @Query("price") String price);

    }


}
